package com.kongfuzi.student.ui.global;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.kongfuzi.student.R;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.ui.base.CustomerActionBarActivity;
import com.kongfuzi.student.ui.gaspay.GasOrderActivity;
import com.kongfuzi.student.ui.gaspay.GasPayActivity;
import com.kongfuzi.student.ui.usercenter.LoginActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HomeActivity extends CustomerActionBarActivity implements View.OnClickListener {

    @Bind(R.id.gv)
    GridView mGv;
    private SlidingMenu menu;
    private String[] itemsName;
    private MenuFragment fragment;
    private int[] imgs = {R.drawable.ranqijiaofei, R.drawable.zhiyingbao, R.drawable.zhilianbao,
            R.drawable.zhifubao, R.drawable.zhixingbao, R.drawable.kefuzhongxin, R.drawable
            .fengkuangguanggao};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        initView();
        initEvents();
    }

    private void initView() {
        initActionBar();
        initMenu();
        itemsName = getResources().getStringArray(R.array.HomeItems);
        mGv.setAdapter(new HomeItemAdapter());
        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0) {
                    if(GasApplication.isLogin()) {
                        startActivity(GasPayActivity.newIntent());
                    } else {
                        startActivity(LoginActivity.newIntent());
                    }
                } else if(position == 2) {
                    startActivity(ZhilianbaoActivity.newIntent());
                } else {
                    startActivity(NothingActivity.newIntent(itemsName[position]));
                }
            }
        });
    }

    private void initEvents() {
        setCustomerBackOnclickListener(this);
        setCustomerOperationOnClickListener(this);
        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menu.isMenuShowing()) {
                    menu.toggle();
                }
            }
        });
    }

    private void initActionBar() {
        setCustomerBackgroundColor(getResources().getColor(R.color.background_home_actionbar));
        setDividerVisibility(false);
        setCustomerTitleColor(getResources().getColor(R.color.text_white));
        setCustomerBackImage(R.drawable.btn_menu);
        setCustomerOperationImageResourse(R.drawable.btn_history);
    }

    private void initMenu() {
        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        menu.setBehindOffset(150);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.menu);
        fragment = new MenuFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.fl_menu, fragment).commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                menu.toggle();
                break;
            case R.id.iv_operation:
                if(GasApplication.isLogin()) {
                    startActivity(GasOrderActivity.newIntent());
                } else {
                    startActivity(LoginActivity.newIntent());
                }
                break;
        }
    }

    private class HomeItemAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return itemsName.length;
        }

        @Override
        public String getItem(int position) {
            return itemsName[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null) {
                convertView = View.inflate(HomeActivity.this,R.layout.layout_item_home_gv,null);
                holder = new ViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.iv);
                holder.tv = (TextView) convertView.findViewById(R.id.tv);
                holder.right = convertView.findViewById(R.id.view_right);
                holder.bottom = convertView.findViewById(R.id.view_bottom);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.right.setVisibility(View.INVISIBLE);
            if((position+1)%3==0) {
                holder.right.setVisibility(View.INVISIBLE);
            } else {
                holder.right.setVisibility(View.VISIBLE);
            }
            holder.iv.setImageResource(imgs[position]);
            holder.tv.setText(itemsName[position]);
            return convertView;
        }
    }

    static class ViewHolder {
        public ImageView iv;
        public TextView tv;
        public View right;
        public View bottom;
    }
}
